Shader "Unlit/InstancedShader"
{
    Properties
    {
        _Color("Color",Color) = (1,1,1,1)
        _MainTex("MainTex",2D) = "gray"{}
    }

    SubShader
    {
        Tags
        {
            "RenderPipeline"="UniversalPipeline"
            "RenderType"="Opaque"
            "Queue"="Geometry+0"
        }

        Pass
        {
            Name "Forward"
			Tags
            { 
			    "LightMode"="UniversalForward"
			}
            
            Blend One Zero,One Zero
            Cull Back
            ZTest LEqual
            ZWrite On

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 4.5

            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

            CBUFFER_START(UnityPerMaterial)
            half4 _Color;
            float4 _MainTex_ST;
            CBUFFER_END

            TEXTURE2D(_MainTex);

            //为了方便操作 定义预定义
            #define smp SamplerState_Point_Repeat
            // SAMPLER(sampler_MainTex); 默认采样器
            SAMPLER(smp);

            #if SHADER_TARGET >= 45
            StructuredBuffer<float4> positionBuffer; // 记录所有待绘制实例的位置缓冲
            #endif
            
            // 顶点输入
            struct Attributes
            {
                float3 positionOS : POSITION;
                float2 uv : TEXCOORD0;
                uint instanceID : SV_InstanceId; // instanceID 实例在缓冲中的索引
            };

            //顶点输出
            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            void rotate2D(inout float2 v, float r)
            {
                float s, c;
                sincos(r, s, c);
                v = float2(v.x * c - v.y * s, v.x * s + v.y * c);
            }

            Varyings vert(Attributes v)
            {
                #if SHADER_TARGET >= 45
                float4 data = positionBuffer[v.instanceID]; // 取出缓冲中的数据，xyz对应世界坐标，w对应缩放系数
                #else
                float4 data = 0;
                #endif

                float rotation = data.w * data.w * _Time.x * 0.5f;
                rotate2D(data.xz, rotation); // 围绕中心点的旋转

                float3 localPosition = v.positionOS.xyz * data.w; // 顶点的模型坐标
                float3 worldPosition = data.xyz + localPosition; // 世界坐标
                
                Varyings o;
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.positionCS = TransformWorldToHClip(worldPosition);
                return o;
            }

            half4 frag(Varyings i) : SV_TARGET
            {
                half4 mainTex = SAMPLE_TEXTURE2D(_MainTex, smp, i.uv);
                half4 c = _Color * mainTex;
                return c;
            }
            ENDHLSL
        }
    }
}
